package com.jf.furn.web;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf.furn.bean.Furn;
import com.jf.furn.service.FurnService;
import com.jf.furn.util.Msg;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-12-17
 * Time:10:37
 */
@RestController
public class FurnController {
    @Resource
    private FurnService furnService;

    @GetMapping("/list")
    public List<Furn> list() {
        return furnService.list();
    }

    @GetMapping("/listPage")
    public Msg listPage(@RequestParam("pageNo") Integer pageNo,
                        @RequestParam("pageSize") Integer pageSize,
                        @RequestParam(value = "name", required = false) String name) {
        Page<Furn> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Furn> furnWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty())
            furnWrapper.like("name", name);
        Page<Furn> pageInfo = furnService.page(page, furnWrapper);
        Msg msg = Msg.success();
        msg.add("pageInfo", pageInfo);
        return msg;
    }

    @GetMapping("/listPage2")
    public Msg listPage2(@RequestParam("pageNo") Integer pageNo,
                         @RequestParam("pageSize") Integer pageSize,
                         @RequestParam(value = "name", required = false) String name) {
        Page<Furn> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Furn> furnWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name))
            furnWrapper.like(Furn::getName, name);
        Page<Furn> pageInfo = furnService.page(page, furnWrapper);
        Msg msg = Msg.success();
        msg.add("pageInfo", pageInfo);
        return msg;
    }
    @PostMapping("/save")
    public Msg add(@Validated @RequestBody Furn furn, Errors errors) {
        if (errors.hasErrors()) {
            List<FieldError> fieldErrors = errors.getFieldErrors();
            HashMap<String, Object> errorMap = new HashMap<>();
            for (FieldError error : fieldErrors) {
                errorMap.put(error.getField(), error.getDefaultMessage());
            }
            Msg msg = Msg.fail(400, "Data validation failed");
            msg.add("errors", errorMap);
            return msg;
        }
        furnService.save(furn);
        return Msg.success();
    }

    @DeleteMapping("/delete")
    public Msg delete(@RequestParam("id") Integer id) {
        furnService.removeById(id);
        return Msg.success();
    }

    @PutMapping("/update")
    public Msg update(@Validated @RequestBody Furn furn) {
        furnService.updateById(furn);
        return Msg.success();
    }
}
